package com.erp.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.DepartmentDAO;
import com.erp.dao.PrivilegeDAO;
import com.erp.dao.RoleDAO;
import com.erp.dao.UserDAO;
import com.erp.domain.TDepartment;
import com.erp.domain.TPrivilege;
import com.erp.domain.TRole;
import com.erp.domain.TUser;
import com.erp.dto.Role;
import com.erp.dto.User;
import com.erp.util.CommonUtils;
import com.erp.util.Dom4JParser;

@Service(value = "initService")
@Transactional
public class InitService {

	@Resource(name = "userDAO")
	private UserDAO userDAO;

	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "roleDAO")
	private RoleDAO roleDAO;

	@Resource(name = "roleService")
	private RoleService roleService;

	@Resource(name = "privilegeDAO")
	private PrivilegeDAO privilegeDAO;

	@Resource(name = "departmentDAO")
	private DepartmentDAO departmentDAO;

	public void init() {

		// ======================新建权限=======================
		List<TPrivilege> menus = new Dom4JParser().getPrivilegeObjs();
		Iterator<TPrivilege> it = menus.iterator();
		while (it.hasNext()) {
			TPrivilege menu = it.next();
			privilegeDAO.save(menu);
		}

		// ================新建角色===================
		TRole tRole = new TRole();
		tRole.setSign("admin");// 角色标识
		tRole.setName("超级管理员");// 角色名称
		tRole.setDescription("拥有最高权限，管理所有资源");// 角色描述
		tRole.setPrivileges(menus);// 角色菜单/权限资源
		roleDAO.save(tRole);
		// TODO 同步到Activiti
		roleService.synToActiviti(new Role(tRole));

		// ======================新建岗位=======================
		TDepartment tDepartment1, tDepartment11, tDepartment2, tDepartment3, tDepartment4, tDepartment5, tDepartment6;
		tDepartment1 = new TDepartment("开发部", null);
		tDepartment11 = new TDepartment("开发二部", tDepartment1);
		tDepartment2 = new TDepartment("测试部", null);
		tDepartment3 = new TDepartment("销售部", null);
		tDepartment4 = new TDepartment("客户部", null);
		tDepartment5 = new TDepartment("人力资源部", null);
		tDepartment6 = new TDepartment("财务部", null);

		departmentDAO.save(tDepartment1);
		departmentDAO.save(tDepartment11);
		departmentDAO.save(tDepartment2);
		departmentDAO.save(tDepartment3);
		departmentDAO.save(tDepartment4);
		departmentDAO.save(tDepartment5);
		departmentDAO.save(tDepartment6);

		// ================新建超级管理员用户===================
		TUser tUser = null;
		List<TRole> tRoles = null;

		tUser = new TUser("陈凯", CommonUtils.generateMd5("admin"));
		tRoles = new ArrayList<TRole>();
		tRoles.add(tRole);
		tUser.settRoles(tRoles);
		tUser.settDepartment(tDepartment1);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("厉成", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment1);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("徐志远", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment4);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("王丽芳", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment1);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("唐冬梅", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment1);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("来晗", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment2);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("林湘", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment2);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("钱伟哈", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment11);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("刘日成", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment11);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

		tUser = new TUser("罗利明", CommonUtils.generateMd5("12345"));
		tUser.settDepartment(tDepartment11);
		userDAO.save(tUser);
		// 同步到Activiti
		userService.synToActiviti(new User(tUser));

	}

}